package com.example.ttl1.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName:MessageService
 * Package:com.example.ttl1.service
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/8/11-21:30
 * @Version:v1.0
 */
@Service
@Slf4j
public class MessageService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Value("${my.exchangeName}")
    private String exchangeName;

    public void sendMsg() {
        //消息体
        MessageProperties messageProperties = new MessageProperties();
        //设置过期毫秒数 15秒
        messageProperties.setExpiration("35000");

        Message message = MessageBuilder.withBody("过期时间TTL".getBytes()).andProperties(messageProperties).build();

        rabbitTemplate.convertAndSend(exchangeName, "info", message);

        log.info("消息发送完毕,发送时间为:{}", new Date());
    }
}
